use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug)]
pub struct FileNode {
    pub filename: String,
    pub size: i64,
    pub children: Vec<Rc<RefCell<FileNode>>>,
    pub parent: Option<Rc<RefCell<FileNode>>>,
}

impl FileNode {
    pub fn add_size(&mut self, size: i64) {
        self.size += size;
        match &self.parent {
            Some(parent) => {
                parent.borrow_mut().add_size(size);
            }
            _ => {

            }
        }
    }
    pub fn get_path(self) -> String {

        let mut parent = self.parent;

        let mut path = Vec::new();

        loop {
            match parent{
                None => {
                    break
                }
                Some(p) => {
                    path.insert(0, p.borrow().filename.clone());          // 使用 fname 的独立值
                    parent = p.borrow().parent.clone();
                }
            }
        }

        path.push(self.filename);
        // println!("parent: {}", path.join("\\"));
        path.join("\\")
    }

}
impl Clone for FileNode {
    fn clone(&self) -> Self {
        FileNode {
            filename: self.filename.clone(),
            size: self.size.clone(),
            children: self.children.clone(),
            parent: self.parent.clone()
        }
    }
}